        var idx = 0;
        
        dojo.require('dojo.parser');
        dojo.require('dojo.data.ItemFileReadStore');
        dojo.require('dojo.data.ItemFileWriteStore');
        dojo.require("dojox.grid.compat._grid.edit");
        dojo.require('dojox.grid.DataGrid');
        dojo.require('dijit.form.Button');
        dojo.require('dijit.Dialog');

		/*
        // stuff needed to define catalog grid
        var catalogArray = {
            identifier: "FDMS_id",
			label: 'FDMS_id',
			items: [<?= implode(", ", $catalog_items) ?>]
        };
        var catalogLayout = [
            { field: "FDMS_id", name: "FDMS ID", width:"80px" },
            { field: "name", name: "Name", width:"auto" },
            { field: "price", name: "Price", width:"80px" }
        ];
        var catalogQuery = { FDMS_id: '*'};
        */
		
        var catalogStore = new dojo.data.ItemFileWriteStore({data:catalogArray});

		/*
        // stuff needed to define cart grid
        var cartArray =  {
            identifier: "id",
			label: 'id',
			items: [<?= implode(", ", $cart_items) ?>]
        };
        var cartLayout = [
            { field: "id", name: "ID", width:"40px", hidden:true },
            { field: "FDMS_id", name: "FDMS ID", width:"130px" },
            { field: "name", name: "Name", width:"auto" },
            { field: "price", name: "Price", width:"auto", editable:true },
            { field: "qty", name: "Quantity", width:"auto", editable:true },
            { field: "total", name: "Total", width:"auto" },
            { field: "editable", name: "Editable", width:"80px" }
        ];
        var cartQuery = { id: '*' };
        */
		
        var cartStore = new dojo.data.ItemFileWriteStore({data:cartArray});

        function itemHasChanged(item, attribute, oldValue, newValue)
        {
            if( oldValue == newValue ) return;
            if( attribute == 'total' ) return;
            var itemPrice = cartGrid.store.getValue( item, 'price' );
            var itemQty = cartGrid.store.getValue( item, 'qty' );
            var newTotal = itemPrice * itemQty;
            cartGrid.store.setValue( item, 'total', newTotal );
        }

        function gridDblClkHandler(e)
        {
            var cartItems = getSelectedCartItems();
            var hasNonEditables = false;
            for( var i=0; i < cartItems.length; i++ )
            {
                var selectedItem = cartItems[i];
                var editable = cartGrid.store.getValues(selectedItem, 'editable');
                if(editable == "NO")
                    hasNonEditables = true;
            }

            if(hasNonEditables)
            {
                cartGrid.edit.cancel();
                alert('You have some non-editable items in your selection.  Please deselect these before trying to edit your shopping cart.');
                return false;
            }
            
            return true;
        }
         
        function getSelectedCatalogItems()
        {
            var items = catalogGrid.selection.getSelected();
            return items;
        }

        function getSelectedCartItems()
        {
            var items = cartGrid.selection.getSelected();
            return items;
        }

/*
        $cart_layout[] = "{ field: 'idx', name: 'IDx', width:'80px', hidden:true }";
        $cart_layout[] = "{ field: 'id', name: 'Price List ID', width:'80px' }";
        $cart_layout[] = "{ field: 'name', name: 'Item Name', width:'auto' }";
        $cart_layout[] = "{ field: 'price', name: 'Price', width:'80px' }";
        $cart_layout[] = "{ field: 'qty', name: 'Price', width:'80px' }";
        $cart_layout[] = "{ field: 'total', name: 'Total', width:'80px' }";
        $cart_layout[] = "{ field: 'type', name: 'Source', width:'120px' }";
 */
        function addSelectedItemToCart()
        {
            var selectedItems = getSelectedCatalogItems();
            if(selectedItems.length<1)
            {
                alert('Please select an item you would like to add.');
                return;
            }

            var cartLength = cartArray.items.length;
            
            for( var i=0; i < selectedItems.length; i++ )
            {
                var selectedItem = selectedItems[i];
                var id = idx++;
				//CHANGEME - change this to reflect the new grids?
                var FDMS_id = catalogGrid.store.getValues(selectedItem, 'FDMS_id');
                var name = catalogGrid.store.getValues(selectedItem, 'name');
                var price = catalogGrid.store.getValues(selectedItem, 'price');
                var qty = 0;
                var total = 0;

                cartGrid.store.newItem({ id: id, FDMS_id: FDMS_id, name:name, price:price, qty:0, total:0, editable:"YES" });
            }                
        }

        function removeSelectedItemFromCart()
        {
            var selectedItems = getSelectedCartItems();
            if(selectedItems.length<1)
            {
                alert('Please select an item you would like to remove.');
                return;
            }

            // in here, check to see if selection contains any
            // non-removable items.  if it does, alert user and book.
            var hasNonEditables = false;
            for( var i=0; i < selectedItems.length; i++ )
            {
                var selectedItem = selectedItems[i];
                var editable = cartGrid.store.getValues(selectedItem, 'editable');
                if(editable == "NO")
                    hasNonEditables = true;
            }

            if(hasNonEditables)
            {
                alert('You have some non-editable items in your selection.  Please deselect these before trying to remove them from the shopping cart.');
                return;
            }

            var selectedItem = selectedItems[0];
            cartGrid.store.deleteItem(selectedItem);
        }

        function submitCart()
        {
            // can also just make this an XML string and send it...
            var xmlStr = '<cartItems>';
            for( var i=0; i < cartArray.items.length; i++ )
            {
                xmlStr = xmlStr + '<cartItem>';
                xmlStr = xmlStr + '<FDMSid>' + cartArray.items[i].FDMS_id + '</FDMSid>';
                xmlStr = xmlStr + '<name>' + cartArray.items[i].name + '</name>';
                xmlStr = xmlStr + '<price>' + cartArray.items[i].price + '</price>';
                xmlStr = xmlStr + '<qty>' + cartArray.items[i].qty + '</qty>';
                xmlStr = xmlStr + '</cartItem>';
            }
            var xmlStr = xmlStr + '</cartItems>';
            alert(xmlStr);
        }

        dojo.addOnLoad( function() {

        catalogGrid = new dojox.grid.DataGrid({
            query: catalogQuery,
            store: catalogStore,
            structure: catalogLayout,
            rowsPerPage: 5,
            selectionMode: 'single'
            }, 'dCatalogGrid');

        cartGrid = new dojox.grid.DataGrid({
            query: cartQuery,
            store: cartStore,
            structure: cartLayout,
            rowsPerPage: 5,
            selectionMode: 'single'
            }, 'dCartGrid');

            catalogGrid.startup();
            cartGrid.startup();

            dojo.connect( cartGrid, "onCellDblClick", gridDblClkHandler );
            dojo.connect( cartGrid.store, "onSet", itemHasChanged );
            
            addPreloadedCartItems();
});
